package com.xieyunjie.controller;

import com.xieyunjie.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: 0812springboot
 * @author: 雷哥
 * @create: 2019-12-16 14:19
 **/
@Controller
@RequestMapping("index")
public class IndexController {

    @RequestMapping("hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping("userManager")
    public String userManager(){
        return "user/userManager";
    }

    @RequestMapping("showStudent")
    public String showStudent(){
        return "showStudent";
    }


    @RequestMapping("showOneStudent")
    public String showOneStudent(Model model){
        Student student=new Student(1,"小明","男",22,"1341113311",new Date(),12312312312.1899123);

        model.addAttribute("student",student);
        return "showOneStudent";
    }

    @RequestMapping("showAllStudent")
    public String showAllStudent(Model model){
        List<Student> students=new ArrayList<>();
        for (int i = 1; i <=5 ; i++) {
            students.add(new Student(i,"小明"+i,i%2==0?"男":"女",22+i,"1341113311"+i,new Date(),12312312312.1899123));
        }
        model.addAttribute("students",students);
        return "showAllStudent";
    }

    @RequestMapping("showObjects")
    public String showObjects(HttpServletRequest request,Model model){

        HttpSession session = request.getSession();

        ServletContext servletContext = request.getServletContext();
        model.addAttribute("name","name_model");
        request.setAttribute("name","name_reqeuest");
        session.setAttribute("name","name_session");
        servletContext.setAttribute("name","name_servletContext");

        return "showObjects";
    }
}
